package pacBioSimple;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;

public class ReadSoapResult {
	
	private HashMap<String, SoapAlignmentObject> alignmentR = null;
	private BufferedReader br =null;
	
	public ReadSoapResult(String file) throws Exception
	{
		br = new BufferedReader(new FileReader(file));
		readFile();
	}
	
	private void readFile() throws Exception
	{
		
		String pacReadName="";
		boolean collision_mode=false;
		int pos=-1;
		alignmentR = new HashMap<String, SoapAlignmentObject>();
		
		HashMap<Integer, HashMap<Character,Integer> > collision=new HashMap<Integer, HashMap<Character,Integer>>();
		
		HashMap<Integer, Character> pacBioReadFinal=new HashMap<Integer, Character>();
		
		HashMap<Integer, Character> pacBioReadRow=new HashMap<Integer, Character>();
		HashMap<Integer, Character> pacBioReadRowTemp=new HashMap<Integer, Character>();
		

		while(br.ready())
		{
			String[] line = br.readLine().split("\t");
			if(pacReadName=="")
			{
				pacReadName=line[7];
				pos=Integer.valueOf(line[8]);
				int indel =Integer.valueOf(line[9]);
				for(int i=0;i<indel;++i)
				{
					String[]values = line[10+i].split("->");
					values=values[1].split("-");
					pacBioReadRowTemp.put(Integer.valueOf(values[0].substring(0,values[0].length()-1))+pos,values[0].charAt(values[0].length()-1));
				}
			}
			else if(!pacReadName.equals(line[7]))
			{
				//evaluate
				if(pacBioReadRowTemp.size()!=0)
				{
					pacBioReadRow.putAll(pacBioReadRowTemp);
					pacBioReadRowTemp.clear();
				}
				evaluate(pacReadName,collision, pacBioReadFinal, pacBioReadRow, alignmentR);
				pacReadName=line[7];
				pos=Integer.valueOf(line[8]);
				int indel =Integer.valueOf(line[9]);
				for(int i=0;i<indel;++i)
				{
					String[]values = line[10+i].split("->");
					values=values[1].split("-");
					if(Integer.valueOf(values[0].substring(0,values[0].length()-1))>=100)
					{
						System.out.println("ERROR");
						System.exit(1);
					}
					pacBioReadRowTemp.put(Integer.valueOf(values[0].substring(0,values[0].length()-1))+pos,values[0].charAt(values[0].length()-1));
				}
				collision_mode=false;
			}
			else
			{
				int newPos=Integer.valueOf(line[8]);
				if(newPos == pos)
				{
					if(!collision_mode)
					{
						MyUtil.copyToDoubleMap(pacBioReadRowTemp, collision);
						pacBioReadRowTemp.clear();
					}
					int indel =Integer.valueOf(line[9]);
					for(int i=0;i<indel;++i)
					{
						String[]values = line[10+i].split("->");
						values=values[1].split("-");
						if(Integer.valueOf(values[0].substring(0,values[0].length()-1))>=100)
						{
							System.out.println("ERROR");
							System.exit(1);
						}
						int mis=Integer.valueOf(values[0].substring(0,values[0].length()-1))+pos;
						
						MyUtil.addToDoubleMap(collision, mis, values[0].charAt(values[0].length()-1));
					}
					collision_mode=true;
				}
				else
				{
					if(!collision_mode)
					{
						pacBioReadRow.putAll(pacBioReadRowTemp);
						pacBioReadRowTemp.clear();
					}
					collision_mode=false;
					//soap alignmentobject erstellen!
					int indel =Integer.valueOf(line[9]);
					for(int i=0;i<indel;++i)
					{
						String[]values = line[10+i].split("->");
						values=values[1].split("-");
						if(Integer.valueOf(values[0].substring(0,values[0].length()-1))>=100)
						{
							System.out.println("ERROR");
							System.exit(1);
						}
						pacBioReadRowTemp.put(Integer.valueOf(values[0].substring(0,values[0].length()-1))+newPos,values[0].charAt(values[0].length()-1));
					}
					pos=newPos;
				}
			}
		}
		if(pacBioReadRowTemp.size()!=0)
		{
			pacBioReadRow.putAll(pacBioReadRowTemp);
			pacBioReadRowTemp.clear();
		}
		evaluate(pacReadName,collision, pacBioReadFinal, pacBioReadRow, alignmentR);		
	}

	
	private void evaluate(String pacReadName, HashMap<Integer,HashMap<Character, Integer>> collision,HashMap<Integer, Character> pacBioReadFinal,HashMap<Integer, Character> pacBioReadRow,HashMap<String, SoapAlignmentObject> alignmentR)
	{
		if(collision.size()!=0)
		{
			//evaluate
			for(Map.Entry<Integer,HashMap<Character, Integer>>entry: collision.entrySet())
			{
				char name=' ';
				int number=1;
				for(Map.Entry<Character, Integer> inner_entry : entry.getValue().entrySet())
				{
					if(number<inner_entry.getValue())
					{
						name=inner_entry.getKey();
						number=inner_entry.getValue();
					}
					else if(number>1 &&number==inner_entry.getValue())
					{
						name=' ';
					}
				}
				if(number != 1 && name!=' ')
				{
					pacBioReadRow.put(entry.getKey(),name);
				}	
			}
		}
		pacBioReadFinal.putAll(pacBioReadRow);
		//soap alignmentobject erstellen!
		pacBioReadRow.clear();
		collision.clear();
		SoapAlignmentObject sao = new SoapAlignmentObject(pacBioReadFinal);
		pacBioReadFinal.clear();
		alignmentR.put(pacReadName, sao);
	}

	public HashMap<String, SoapAlignmentObject> getAlignmentR() {
		return alignmentR;
	}

	public void setAlignmentR(HashMap<String, SoapAlignmentObject> alignmentR) {
		this.alignmentR = alignmentR;
	}

	public BufferedReader getBr() {
		return br;
	}

	public void setBr(BufferedReader br) {
		this.br = br;
	}
	

}
